package com.linjuke.water.core.services.impl;

import java.util.List;

import com.linjuke.water.common.domains.ItemDO;
import com.linjuke.water.common.services.ItemService;
import com.linjuke.water.common.services.query.ItemQuery;
import com.linjuke.water.common.services.query.RegionItemQuery;
import com.linjuke.water.common.services.query.UserItemQuery;
import com.linjuke.water.common.services.result.ListResult;
import com.linjuke.water.core.dal.datainterface.ItemDAO;

public class ItemServiceImpl implements ItemService {
	
	private ItemDAO itemDAO;

	public long create(ItemDO itemDO) {
		return itemDAO.create(itemDO);
	}

	public void delete(long id) {
		itemDAO.delete(id);
	}

	public void update(ItemDO itemDO) {
		itemDAO.update(itemDO);
	}

	public ItemDO queryById(long id) {
		return itemDAO.queryById(id);
	}

	public ListResult<ItemDO> queryForPage(ItemQuery itemQuery) {
		ListResult<ItemDO> ret = new ListResult<ItemDO>();
		List<ItemDO> items = itemDAO.queryForPage(itemQuery);
		ret.setRecords(items);
		ret.setNumFound(itemQuery.getTotalResultCount());
		return ret;
	}
	
	@Override
	public ListResult<ItemDO> queryForUserItems(UserItemQuery userItemQuery) {
		ListResult<ItemDO> ret = new ListResult<ItemDO>();
		List<ItemDO> items = itemDAO.queryForUserItems(userItemQuery);
		ret.setRecords(items);
		return ret;
	}

	public ListResult<ItemDO> queryForRegionItems(RegionItemQuery regionItemQuery) {
		ListResult<ItemDO> ret = new ListResult<ItemDO>();
		List<ItemDO> items = null;
		if(regionItemQuery.isMoreRegion()) {
			items = itemDAO.queryForRegionItemsSecond(regionItemQuery);
		} else {
			items = itemDAO.queryForRegionItemsFirst(regionItemQuery);
		}
		ret.setRecords(items);
		ret.setNumFound(regionItemQuery.getTotalResultCount());
		return ret;
	}

	public void setItemDAO(ItemDAO itemDAO) {
		this.itemDAO = itemDAO;
	}

}
